package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.DictionaryItem;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.PlanDevelopment;
import cn.wolfcode.crm.mapper.ChangeClientLogMapper;
import cn.wolfcode.crm.mapper.ClientMapper;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.mapper.PlanDevelopmentMapper;
import cn.wolfcode.crm.query.ClientQueryObject;
import cn.wolfcode.crm.service.IClientService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @program: crm
 * @description:
 * @author: Mr.chen
 * @create: 2018-07-07 21:45
 **/
@Service
public class ClientServiceImpl implements IClientService {
    @Autowired
    private ClientMapper mapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ChangeClientLogMapper changeClientLogMapper;
    @Autowired
    private PlanDevelopmentMapper planDevelopmentMapper;

    public void save(Client entity) {

      //获取当前用户信息
       Employee user = (Employee) SecurityUtils.getSubject().getPrincipal();

        //设置客户信息创建人信息并且设置录入时间
        entity.setCreateUser(user);
        entity.setCreateDate(new Date());

        //设置客户负责人信息并且设置接手时间
        entity.setLeadingUser(user);
        entity.setLeadingDate(new Date());

        mapper.insert(entity);
    }

    public void update(Client entity) {
        mapper.updateByPrimaryKey(entity);
    }

    public void delete(long id) {
        mapper.deleteByPrimaryKey(id);
    }

    public Client get(long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<Client> list() {
        return mapper.selectAll();
    }

    @Override
    public PageResult query(ClientQueryObject qo) {
        int total = mapper.queryForCount(qo);
        if (total == 0) {
            return new PageResult();
        }
        List<Client> list = mapper.queryForList(qo);
        return new PageResult(total, list);
    }

    public void changeState(Long id, int state) {
        if (state == 4) {
            Client client = mapper.selectByPrimaryKey(id);

            PlanDevelopment planDevelopment = new PlanDevelopment();

            planDevelopment.setClient(client);
            planDevelopment.setCreateDate(new Date());

            //获取当前用户信息
            Employee user = (Employee) SecurityUtils.getSubject().getPrincipal();
            planDevelopment.setInput(user);
            DictionaryItem implement = planDevelopment.getImplement();
            //设置实施方式默认值
            implement.setId(36L);
            planDevelopment.setImplement(implement);
            planDevelopmentMapper.insert(planDevelopment);
        }
        if (state == 5) {
            Client client = mapper.selectByPrimaryKey(id);


            planDevelopmentMapper.deleteByClientKey(client.getId());
        }

        if (state == 2) {
            Client client = mapper.selectByPrimaryKey(id);

            planDevelopmentMapper.deleteByClientKey(client.getId());
            }

            mapper.changeState(id, state);
    }
}
